<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>封装promise定时器</title>
</head>
<body>
    
</body>
<script>
    function timeout(delay = 1000){
       return new Promise((resolve, reject) => {
            setTimeout(() => {
                try{
                resolve();
                }
                //异步发生的错误无法被catch直接捕获
                catch(error){
                    reject(error); //使用reject
                }
            }, delay);
        });
    }
    
    //promise封装setTimeout
    timeout(2000).then(() => {
        console.log('2s later do something');
        return timeout(1000)
    }).then(() => {
        console.log('1s later do something');
    }).catch((error) => {
        console.log('something wrong has been happend!!', error)
    })

    
    
</script>
</html>
